package com.xnyzc.lhy.common.exception;

import com.xnyzc.lhy.common.entity.Sb;
import com.xnyzc.lhy.common.penum.invoice.EInvoiceErrorCode;
import lombok.Data;

/**
 * 发票对接异常
 */
@Data
public class InvoiceException extends RuntimeException {
    /**
     * 定义枚举
     */
    private EInvoiceErrorCode errno;
    /**
     * 问题描述
     */
    private String errmsg;

    /**
     * 构造函数
     *
     * @param errno
     */
    public InvoiceException(EInvoiceErrorCode errno) {
        super(errno.toChString());

        this.errno = errno;
        this.errmsg = errno.toChString();
    }

    /**
     * 构造函数
     *
     * @param errno
     * @param errmsg
     */
    public InvoiceException(EInvoiceErrorCode errno, String errmsg) {
        super(errmsg);

        this.errno = errno;
        this.errmsg = errmsg;
    }

    public InvoiceException(Exception e) {
        super(e);

        this.errno = EInvoiceErrorCode.unKnow;
        this.errmsg = "";
    }

    public InvoiceException(String errmsg) {
        super(errmsg);

        this.errno = EInvoiceErrorCode.unKnow;
        this.errmsg = "";
    }

    /**
     * 构造函数
     *
     * @param errno
     * @param errmsg
     */
    public InvoiceException(EInvoiceErrorCode errno, String errmsg, Exception e) {
        super(errmsg, e);

        this.errno = errno;
        this.errmsg = errmsg;
    }

    public static InvoiceException create(EInvoiceErrorCode errno) {
        return new InvoiceException(errno);
    }

    public static InvoiceException create(EInvoiceErrorCode errno, String errmsg, Object... args) {
        return new InvoiceException(errno, Sb.format(errmsg, args));
    }

    public static InvoiceException create(Exception e) {
        return new InvoiceException(e);
    }

    public static InvoiceException create(String errmsg) {
        return new InvoiceException(errmsg);
    }

    /**
     * 打印日志
     *
     * @return
     */
    public String printLog() {
        return new StringBuilder().append("bop exception, type:").append(this.errno.toChString()).append(" errmsg:").append(this.errmsg)
                .toString();
    }
}
